import md5Libs from '@/request/md5.js';
export default class Utils {
    private env: string = 'development';
    private baseUrl: string = 'https://test.center.ugrow.cn/RD-lepop';
    private devBaseUrl: string = 'https://test.center.ugrow.cn/RD-lepop';
    private sign: string = '';
    constructor() {
        this.setBaseUrl();
    }
    // 设置baseUrl
    public setBaseUrl() {
        if (process.env.NODE_ENV === 'development') {
            try {
                const node_env = uni.getStorageSync('node_env');
                if (node_env) {
                    this.env = node_env.env;
                    this.devBaseUrl = node_env.DEV_BASE_URL;
                } else {
                    uni.setStorage({
                        key: 'node_env',
                        data: {
                            env: this.env,
                            DEV_BASE_URL: this.devBaseUrl,
                        },
                    });
                }
            } catch (e) {
                // error
            }
        } else {
            this.env = 'product';
            this.baseUrl = 'https://test.center.ugrow.cn/RD-lepop';
        }
        switch (this.env) {
            case 'development':
                this.baseUrl = this.devBaseUrl;
                break;
            case 'test':
                this.baseUrl = 'https://test.center.ugrow.cn/RD-lepop';
                break;
            default:
                this.baseUrl = 'https://www.lepop.store';
                break;
        }
    }
    public getBaseUrl(): string {
        return this.baseUrl;
    }
    // 生成签名
    public setSign(obj: any, security_key: string) {
        const utils = new Utils();
        obj['sign_time'] = parseInt((new Date().getTime() / 1000).toString());
        let newObj = {
            ...obj,
        };
        newObj['security_key'] = security_key;
        let sortArr: any = [];
        let keysSorted = Object.keys(newObj).sort(); //排序名
        for (let i = 0; i < keysSorted.length; i++) {
            sortArr.push(keysSorted[i] + '=' + newObj[keysSorted[i]]);
        }
        sortArr = sortArr.join('&');
        utils.sign = md5Libs.md5(sortArr);
        return utils;
    }
    
    public getSign(): string {
        return this.sign;
    }
    
 }